<?php
    $open_graph = [
        'title' => _f('%s on %s', $user->username, get_app_configuration('brand')),
        'description' => _f('Discover %s’s collections on %s.', $user->username, get_app_configuration('brand')),
        'locale' => $user->locale,
        'url' => url_full('profile', ['id' => $user->id]),
    ];
    if ($user->avatar_filename) {
        $open_graph['image'] = url_media_full('avatars', $user->avatar_filename);
    }

    $this->layout('base.phtml', [
        'title' => $user->username,
        'canonical' => url_full('profile', ['id' => $user->id]),
        'current_page' => $is_current_user_profile ? 'profile' : '',
        'alternates' => [
            [
                'type' => 'application/atom+xml',
                'url' => url_full('profile feed', ['id' => $user->id]),
                'title' => _f('Syndication feed of %s', $user->username),
            ],
            [
                'type' => 'text/x-opml',
                'url' => url_full('profile opml', ['id' => $user->id]),
                'title' => _f('Collections OPML of %s', $user->username),
            ],
        ],
        'open_graph' => $open_graph,
    ]);
?>

<div class="section section--longbottom">
    <img class="avatar avatar--banner" src="<?= url_avatar($user->avatar_filename) ?>" alt="" />

    <div class="section__title">
        <h1><?= protect($user->username) ?></h1>
    </div>

    <?php if ($is_current_user_profile): ?>
        <div class="profile__meta">
            <p class="profile__details">
                <?= _('This is your public profile!') ?>
            </p>

            <div class="profile__actions">
                <button
                    class="button--small"
                    data-controller="modal-opener"
                    data-action="modal-opener#fetch"
                    data-modal-opener-href-value="<?= url('edit profile', ['from' => url('profile', ['id' => $user->id])]) ?>"
                    aria-haspopup="dialog"
                    aria-controls="modal"
                >
                    <?= icon('pencil') ?>
                    <?= _('Edit') ?>
                </button>
            </div>
        </div>
    <?php endif; ?>

    <?php if ($links): ?>
        <h2 class="group__subtitle">
            <?= _('Last shared links') ?>
        </h2>

        <div class="cards">
            <?php foreach ($links as $link): ?>
                <?= $this->include('links/_link.phtml', [
                    'link' => $link,
                    'from' => \Minz\Url::for('profile', ['id' => $user->id]),
                    'display_notes' => true,
                    'display_edit' => $is_current_user_profile,
                    'display_repair' => $is_current_user_profile,
                    'display_delete' => $is_current_user_profile,
                    'display_read_later' => $current_user ? 'auto' : false,
                    'display_mark_as_read' => $current_user ? 'auto' : false,
                ]) ?>
            <?php endforeach; ?>
        </div>
    <?php elseif ($is_current_user_profile): ?>
        <p class="paragraph--placeholder">
            <?= _f('You didn’t share any links yet. Start by <a href="%s">creating a public collection</a>.', url('links')) ?>
        </p>
    <?php else: ?>
        <p class="paragraph--placeholder">
            <?= _('This user didn’t share any links yet.') ?>
        </p>
    <?php endif; ?>

    <?php if ($collections): ?>
        <h2 class="group__subtitle">
            <?= _('Published collections') ?>
        </h2>

        <div class="cards">
            <?php foreach ($collections as $collection): ?>
                <?= $this->include('collections/_collection.phtml', [
                    'collection' => $collection,
                    'display_by' => false,
                    'display_number_links' => true,
                    'display_visibility' => false,
                ]) ?>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>

    <?php if ($shared_collections): ?>
        <h2 class="group__subtitle">
            <?= _('Collections shared with you') ?>
        </h2>

        <div class="cards">
            <?php foreach ($shared_collections as $collection): ?>
                <?= $this->include('collections/_collection.phtml', [
                    'collection' => $collection,
                    'display_by' => false,
                    'display_number_links' => true,
                    'display_visibility' => false,
                ]) ?>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</div>
